package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.AnnouncementVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.UserInfoVo;
import com.tanhua.dubbo.api.MessagesApi;
import com.tanhua.dubbo.api.UserInfoApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class MessagesService {

    @Reference
    private MessagesApi messagesApi;

    @Reference
    private UserInfoApi userInfoApi;

    //公告列表业务
    public ResponseEntity<Object> queryAnnouncements(Integer page, Integer pagesize) throws InvocationTargetException, IllegalAccessException {
        IPage<Announcement> iPage = messagesApi.findAnnouncements(page, pagesize);
        List<Announcement> records = iPage.getRecords();
        List<AnnouncementVo> list = new ArrayList<>();
        for (Announcement announcement : records) {
            if (announcement != null) {
                AnnouncementVo announcementVo = new AnnouncementVo();
                BeanUtils.copyProperties(announcementVo, announcement);
                announcementVo.setId(announcement.getId().toString());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String createDate = sdf.format(announcement.getCreated());
                announcementVo.setCreateDate(createDate);
                list.add(announcementVo);
            }
        }

        PageResult pageResult = new PageResult(page, pagesize, (int) iPage.getTotal(), list);
        return ResponseEntity.ok(pageResult);
    }


}
